home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / getcwd.c < prev    next >
C/C++ Source or Header  |  1992-02-11  |  1KB  |  59 lines

  1. #include <stddef.h>
  2. #include <stdlib.h>    /* both of these added for malloc() */
  3. #include <limits.h>
  4. #include <errno.h>
  5. #include <osbind.h>
  6. #include "lib.h"
  7.  
  8. /*******************************************************************
  9. getcwd: returns current working directory. By ERS.
  10. This routine is in the public domain.
  11. ********************************************************************/
  12.  
  13. extern int __mint;
  14. extern char _rootdir;    /* in main.c: user's preferred root directory */
  15.  
  16. char *getcwd(buf, size)
  17. char *buf; int size;
  18. {
  19.     char _path[PATH_MAX];
  20. /* sometimes the GCC optimizer is too clever; it doesn't know that
  21.    Dgetpath(path, 0) can change the stuff path points to
  22.  */
  23.     volatile char *path;
  24.     char drv;
  25.  
  26.     if (!buf)
  27.         if (!(buf = (char *) malloc((size_t)size)))
  28.             return NULL;
  29.  
  30.     drv = Dgetdrv() + 'a';
  31.     _path[0] = drv;
  32.     _path[1] = ':';
  33.     _path[2] = '\0';
  34.     path = _path + 2;
  35.     
  36.     (void)Dgetpath(path, 0);
  37.  
  38.     if (_rootdir && drv == _rootdir) {
  39.         if (!*path) {
  40.             path[0] = '\\';
  41.             path[1] = '\0';
  42.         }
  43.         _dos2unx((char *)path, buf);
  44.         return buf;
  45.     }
  46.     _dos2unx(_path, buf);    /* convert DOS filename to unix */
  47.     return buf;
  48. }
  49.  
  50. /*
  51.  * char *getwd(char *buf)
  52.  *    return cwd in buf
  53.  */
  54. char *getwd(buf)
  55. char *buf;
  56. {
  57.     return getcwd(buf, PATH_MAX);
  58. }
  59.